package top.wettis.chat.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.wettis.chat.constant.ResultType;
import top.wettis.chat.pojo.DO.Objectives;
import top.wettis.chat.pojo.VO.Result;
import top.wettis.chat.service.IObjectivesService;

import java.util.List;

/**
 * <p>
 * 目标 前端控制器
 * </p>
 *
 * @author wettis
 * @since 2023-03-25
 */
@RestController
@CrossOrigin
@RequestMapping("/chat")
public class ObjectivesController {
    @Autowired
    private IObjectivesService iObjectivesService;

    @GetMapping("/GetallDaysPlan")
    public List<Objectives> getall() {
        int ui = StpUtil.getLoginIdAsInt();
        ;
        return iObjectivesService.selectAll(ui);
    }

    @GetMapping("/progress")
    public Result<Object> getTotalProgress() {
        int userId = StpUtil.getLoginIdAsInt();
        LambdaQueryWrapper<Objectives> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objectives::getUserId, userId);
        long total = iObjectivesService.count(wrapper);
        wrapper.eq(Objectives::getFinished, true);
        long count = iObjectivesService.count(wrapper);
        double progress = count * 100.0 / total;
        return Result.successInfo(ResultType.GET,progress);
    }

}
